# -*- coding: utf-8 -*-
import scrapy
from csdnslaver.items import CsdnslaveItem
from scrapy_redis.spiders import RedisSpider
import re

class CourseSpider(RedisSpider):
    name = 'course'
    # allowed_domains = ['https://edu.csdn.net']
    # start_urls = ['http://https://edu.csdn.net/']

    redis_key='coursespider:start_urls'

    def __init__(self, *args, **kwargs):
        # Dynamically define the allowed domains list.
        domain = kwargs.pop('domain', '')
        self.allowed_domains = filter(None, domain.split(','))
        super(CourseSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        #print(response.status)
        '''
        response=response.xpath("//body")
        item = CsdnslaveItem()
        item['title'] = response.xpath(".//*[@id='course_detail_block1']/div/div/h1/text()").extract_first()
        item['teacher'] = response.xpath('.//*[@id="block2_right"]/div[2]/div[1]/div[2]/a/text()').extract_first()
        item['coursehour'] = response.re('<span class="pinfo">(.*?)</span>')
        item['usergroup'] = response.xpath(".//div[@class='forwho']/span/text()").extract()
        item['usernum'] = response.xpath(".//*[@id='course_detail_block1']/div/div/div/span/text()").extract_first()
        item['price'] = response.re('<sapn class="money">(.*?)</sapn>')[0]
        item['outline'] = response.xpath('.//div/div/div/div/div/div//div').extract()
        yield item
        #/html/body/div/div/div/div/div/div/div/dl/dt
        '''
        response=response.css("body")
        outline_list=response.css('dl.outline_list')
        item = CsdnslaveItem()
        item['title'] = response.css("div.info_right h1::text").extract_first()
        item['teacher'] = response.css('div.professor_name a::text').extract_first()
        item['usernum'] = response.css("div.course_status span::text").extract_first()
        item['price'] = response.re('<sapn class="money">(.*?)</sapn>')[0]
        #获取大纲列表，并转换成str
        outlinelist=""
        for outline in outline_list:
            outlinelist += outline.css('dt span::text').extract_first().strip()+"--"
            #print('大纲==',type(outline.css('dt span::text').extract_first()))
        item['outline'] = outlinelist
        yield item
